import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:jpush_flutter/jpush_flutter.dart';

import './router/router.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  void initState() { 
    super.initState();
    initJpush();
  }

  /// 监听极光推送
  initJpush() {
    JPush jpush = JPush();

    ///初始化
    jpush.setup(
      appKey: '67007c23db3f7c64788c703f',
      channel: 'theChannel',
      production: false,
      debug: false // 设置是否打印debug日志
    );

    /// 设置别名 指定用户推送
    // jpush.setAlias('limi').then((value) => print('设置别名成功'));

    /// 获取注册的ID
    jpush.getRegistrationID().then((id) => print('获取注册的id:$id'));

    /// 监听消息通知
    jpush.addEventHandler(
      /// 接收通知回调方法
      onReceiveNotification: (Map<String, dynamic> message) async {
        print("flutter onReceiveNotification: $message");
      }, 
      /// 点击通知回调方法
      onOpenNotification: (Map<String, dynamic> message) async {
        print("flutter onOpenNotification: $message");
      }, 
      /// 接收自定义消息回调方法
      onReceiveMessage: (Map<String, dynamic> message) async {
        print("flutter onReceiveMessage: $message");
      },
      // onReceiveNotificationAuthorization: (Map<String, dynamic> message) async {
      //   print("flutter onReceiveNotificationAuthorization: $message");
      // }
    );
  }

  @override
  Widget build(BuildContext context) {
    return  MaterialApp(
      initialRoute: '/',
      onGenerateRoute: onGenerateRoute,
      theme: ThemeData(
        primaryColor: Colors.blue
      ),
      debugShowCheckedModeBanner: false,
      localizationsDelegates: [
        //此处 
        GlobalMaterialLocalizations.delegate, 
        GlobalWidgetsLocalizations.delegate
      ],
      supportedLocales: [ 
        //此处 
        const Locale('zh', 'CH'), 
        const Locale('en', 'US')
      ]
    );
  }
}
